package cn.hlife.com.hlfhaemocyte.measure.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.inuker.bluetooth.library.Constants;

import org.json.JSONException;
import org.json.JSONObject;

import cn.hlife.com.hlfhaemocyte.MainActivity;
import cn.hlife.com.hlfhaemocyte.R;
import cn.hlife.com.hlfhaemocyte.TestViewActivity;
import cn.hlife.com.hlfhaemocyte.bean.MeasureEntity;
import cn.hlife.com.hlfhaemocyte.bluetooth.ClientManager;
import cn.hlife.com.hlfhaemocyte.notify.Transporter;
import cn.hlife.com.hlfhaemocyte.notify.Watcher;
import cn.hlife.com.hlfhaemocyte.utils.LogUtils;
import cn.hlife.com.hlfhaemocyte.utils.SharedPreferencesUtils;
import cn.hlife.com.hlfhaemocyte.utils.TransformDataUtils;

/**
 * Created by Administrator on 2017/6/23.
 */

public class MeasureFragment extends Fragment implements View.OnClickListener{

//    TextView measureRecord;
    Animation decelerateAnim;
    TextView fullBlood, diluent;

    private EditText mUserName;
    private EditText mSpecimen;
    private EditText mDoctor;
    private EditText mAge;
    private EditText mHospitalBedNumber;
    private EditText mTime;
    private EditText mMedicalNumber;
    private EditText mClinicalDiagnosis;
    private EditText mSampleID;
    private EditText mRemark;
    private EditText mSection;

    private RadioGroup radioGroup;
    private String userSex = "男";
    public static final int MEASURE_QX = 101;
    public static final int MEASURE_XSY = 102;
    public static final int MEASURE_YXS = 103;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.measure_fragment, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LogUtils.e("测量 ");
        radioGroup = (RadioGroup) view.findViewById(R.id.radio_group);
        mUserName = (EditText) view.findViewById(R.id.user_name);
        mSpecimen = (EditText) view.findViewById(R.id.specimen);
        mSection = (EditText) view.findViewById(R.id.section);
        mDoctor = (EditText) view.findViewById(R.id.doctor);
        mAge = (EditText) view.findViewById(R.id.age);
        fullBlood = (TextView) view.findViewById(R.id.full_blood);
        diluent = (TextView) view.findViewById(R.id.diluent);
        mHospitalBedNumber = (EditText) view.findViewById(R.id.hospital_bed_number);
        mMedicalNumber = (EditText) view.findViewById(R.id.medical_number);
        mClinicalDiagnosis = (EditText) view.findViewById(R.id.clinical_diagnosis);
        mTime = (EditText) view.findViewById(R.id.time);
        mSampleID = (EditText) view.findViewById(R.id.sample_id);
        mRemark = (EditText) view.findViewById(R.id.remark);

        ((MainActivity)getActivity()).middleText.setText("血细胞分析仪");
        ((MainActivity)getActivity()).rightText.setOnClickListener(this);
        ((MainActivity)getActivity()).rightText.setText("测量记录");
        ((MainActivity)getActivity()).returnImageView.setVisibility(View.GONE);
        fullBlood.setOnClickListener(this);
        diluent.setOnClickListener(this);
        radioGroup.check(R.id.radio_man);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.radio_man:
                        userSex = "男";
                        break;
                    case R.id.radio_woman:
                        userSex = "女";
                        break;
                }
            }
        });
        ((MainActivity) getActivity()).MEASURE_MODE = MEASURE_QX;
        SharedPreferencesUtils.getInstance().saveMode(getContext(),"QUANXYE");
//        view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(getActivity(), TestViewActivity.class);
//                startActivity(intent);
//            }
//        });
//        view.findViewById(R.id.button4).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                String byteStr = "aa03090101";
//                byte[] by = TransformDataUtils.hexStringToByteArray(byteStr);
//                ((MainActivity) getActivity()).writeCommandNoDialog(by);
//            }
//        });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            MeasureEntity md = ((MainActivity) getActivity()).measureEntity;
            md.setMAge(mAge.getText().toString());
            md.setMDoctor(mDoctor.getText().toString());
            md.setMClinicalDiagnosis(mClinicalDiagnosis.getText().toString());
            md.setMHospitalBedNumber(mHospitalBedNumber.getText().toString());
            md.setMMedicalNumber(mMedicalNumber.getText().toString());
            md.setMRemark(mRemark.getText().toString());
            md.setMSampleID(mSampleID.getText().toString());
            md.setMSpecimen(mSpecimen.getText().toString());
            md.setMTime(mTime.getText().toString());
            md.setUserSex(userSex);
            md.setMUserName(mUserName.getText().toString());
            ((MainActivity)getActivity()).rightText.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        switch (viewId) {
            case R.id.full_blood:
                String  macAddress = readBluetoothMAC("macBluetooth");
            int  tt=   ClientManager.getClient().getConnectStatus(macAddress);
                if(ClientManager.getClient().getConnectStatus(macAddress)== Constants.STATUS_DEVICE_CONNECTED){
                    ((MainActivity) getActivity()).MEASURE_MODE = MEASURE_QX;
                    measureModeUIchange(MEASURE_QX);
                }else {
                    Toast.makeText(getContext(),"蓝牙未连接",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.diluent:
                String  macAddress2 = readBluetoothMAC("macBluetooth");
                if(ClientManager.getClient().getConnectStatus(macAddress2)== Constants.STATUS_DEVICE_CONNECTED){
                    if (((MainActivity) getActivity()).MEASURE_MODE == MEASURE_QX) {
                        ((MainActivity) getActivity()).MEASURE_MODE = MEASURE_XSY;
                        measureModeUIchange(MEASURE_XSY);
                    } else if (((MainActivity) getActivity()).MEASURE_MODE == MEASURE_XSY) {
                        ((MainActivity) getActivity()).MEASURE_MODE = MEASURE_YXS;
                        measureModeUIchange(MEASURE_YXS);
                    } else if (((MainActivity) getActivity()).MEASURE_MODE == MEASURE_YXS) {
                        ((MainActivity) getActivity()).MEASURE_MODE = MEASURE_XSY;
                        measureModeUIchange(MEASURE_XSY);
                    }
                }else {
                    Toast.makeText(getContext(),"蓝牙未连接",Toast.LENGTH_SHORT).show();
                }

                break;

        }
    }

    /**
     * 读取蓝牙的MAC的地址
     */
    public String readBluetoothMAC(String key) {
        String macStr = SharedPreferencesUtils.getInstance().getBluetoothAddress(getContext());
        String macAddress = null;
        try {
            if (!TextUtils.isEmpty(macStr)) {
                JSONObject jsonObject = new JSONObject(macStr);
                macAddress = jsonObject.getString(key);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return macAddress;
    }
    public void measureModeUIchange(int state) {
        switch (state) {
            case MEASURE_QX:
                fullBlood.setTextColor(getResources().getColor(R.color.white));
                diluent.setTextColor(getResources().getColor(R.color.text_black));
                fullBlood.setBackgroundResource(R.drawable.select_shape);
                diluent.setBackgroundResource(R.drawable.nor_shape);
                diluent.setText("打稀释液");
                //TODO  需要修改
                SharedPreferencesUtils.getInstance().saveMode(getContext(),"QUANXYE");
                ((MainActivity) getActivity()).writeCommand(new byte[]{(byte) 0xaa, 0x03, 0x09, 0x01, 0x00});
                break;
            case MEASURE_XSY:
                diluent.setText("打稀释液");
                diluent.setTextColor(getResources().getColor(R.color.white));
                fullBlood.setTextColor(getResources().getColor(R.color.text_black));
                diluent.setBackgroundResource(R.drawable.select_shape);
                fullBlood.setBackgroundResource(R.drawable.nor_shape);

                ((MainActivity) getActivity()).writeCommand(new byte[]{(byte) 0xaa, 0x03, 0x09, 0x04, 0x00});
                break;
            case MEASURE_YXS:
                diluent.setText("预稀释");
                //TODO  需要修改
                SharedPreferencesUtils.getInstance().saveMode(getContext(),"YUXISHI");
                diluent.setTextColor(getResources().getColor(R.color.white));
                diluent.setBackgroundResource(R.drawable.select_shape);
                fullBlood.setTextColor(getResources().getColor(R.color.text_black));
                fullBlood.setBackgroundResource(R.drawable.nor_shape);
                ((MainActivity) getActivity()).writeCommand(new byte[]{(byte) 0xaa, 0x03, 0x09, 0x01, 0x00});
                break;
        }
    }

    public String getUserSex() {
        return userSex;
    }

    public EditText getmUserName() {
        return mUserName;
    }

    public EditText getmSpecimen() {
        return mSpecimen;
    }

    public EditText getmDoctor() {
        return mDoctor;
    }

    public EditText getmAge() {
        return mAge;
    }

    public EditText getmHospitalBedNumber() {
        return mHospitalBedNumber;
    }

    public EditText getmTime() {
        return mTime;
    }

    public EditText getmMedicalNumber() {
        return mMedicalNumber;
    }

    public EditText getmClinicalDiagnosis() {
        return mClinicalDiagnosis;
    }

    public EditText getmSampleID() {
        return mSampleID;
    }

    public EditText getmRemark() {
        return mRemark;
    }

    public EditText getmSection() {
        return mSection;
    }


}
